<template>
  <div>
    <van-nav-bar title="黑马头条 - 登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{ required: true, message: '请填写手机号', trigger: 'onChange', pattern: /^1[3456789]\d{9}$/  }]"
      />
      <van-field
        v-model="formLogin.code"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
        :rules="[{ required: true, message: '请填写密码', trigger: 'onChange', pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px;">
       <van-button
            round
            block
            type="info"
            native-type="submit"
            :loading="isLoading"
            :disabled="isLoading"
            loading-text="加载中">登录</van-button>

      </div>
    </van-form>
  </div>
</template>

<script>
// 引入调用
// import { loginAPI } from '@/api'
import { Notify } from 'vant'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      formLogin: {
        mobile: '13888888888',
        code: '246810'
      },
      isLoading: false // 加载状态
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    // 调用接口的步骤
    // 1,引入接口
    // 2，在方法中调用接口
    // 3，调用接口时如果需要传参数，需要将参数整理传入保证参数的数据类型和数据格式
    // 4，定义变量接受返回值
    async onSubmit () {
      this.isLoading = true
      try {
        await this.asyncLoginAction(this.formLogin)
        Notify({ type: 'success', message: '登录成功' })
        this.isLoading = false
        this.$router.push('/layout')
      } catch (err) {
        console.error(err)
        this.isLoading = false
        Notify({ type: 'warning', message: '手机号或密码错误' })
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>
